define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index' + location.search,
                    add_url: 'order/order/add',
                    // edit_url: 'order/order/edit',
                    del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                // queryParams: function (params) {
                //     //这里可以追加搜索条件
                //     var filter = JSON.parse(params.filter);
                //     var op = JSON.parse(params.op);
                //     //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                //     filter.order_status = '-1';
                //     op.order_status = ">";
                //     params.filter = JSON.stringify(filter);
                //     params.op = JSON.stringify(op);
                //     return params;
                // },
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'user.nickname', title: __('User_id') },
                        { field: 'sn', title: __('Sn'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'paytime', title: __('Paytime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // {field: 'contact', title: __('Contact'), operate: 'LIKE'},
                        // {field: 'telephone', title: __('Telephone'), operate: 'LIKE'},
                        // {field: 'address', title: __('Address'), operate: 'LIKE'},
                        { field: 'order_status', title: __('Order_status'), searchList: { "0": __('Order_status 0'), "10": __('Order_status 10'), "20": __('Order_status 20'), "30": __('Order_status 30'), "-1": __('Order_status -1') }, formatter: Table.api.formatter.status },
                        { field: 'type', title: __('订单类型'), searchList: { "1": __('百村联盟'), "2": __('创业专区') }, formatter: Table.api.formatter.normal },
                        { field: 'sendtime', title: __('Sendtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'total_fee', title: __('应付金额'), operate: 'BETWEEN' },
                        { field: 'pay_fee', title: __('支付金额'), operate: 'BETWEEN' },
                        // { field: 'yue', title: __('Yue'), operate: 'BETWEEN' },
                        { field: 'discount_amount', title: __('Discount_amount'), operate: 'BETWEEN' },
                        { field: 'taketime', title: __('Taketime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'content', title: __('Content'), operate: 'LIKE' },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'select',
                                    title: '查看',
                                    text: '查看',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/edit',//接口地址 控制器名/方法名
                                },
                                {
                                    name: 'query',
                                    title: '物流',
                                    text: '物流',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/query?express_com={express_com}&express_no={express_no}',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.order_status > '10') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'deliver',
                                    title: '发货',
                                    text: '发货',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/deliver',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.order_status == '10' || row.order_status == '20') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }

                                    },
                                },
                                {
                                    name: 'custom',
                                    title: '定制金额',
                                    text: '定制金额',
                                    extend: 'data-area=["90%","90%"]',
                                    refresh: true, //操作完成后是否刷新列表
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/custom',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.type != '1' && row.order_status == '0') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }

                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        custom: function () {
            Controller.api.bindevent();
        },
        deliver:function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
